/* assassin.c
 *
 * Copyright 2007-2008 OpenMoko, Inc.
 * Authored by Tick <tick@openmoko.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <Ecore_Evas.h>
#include <Ecore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <assassin.h>
#include <gears.h>
#include <browser.h>
#include <debug.h>

#include <pkutil.h>
#include <confirm_command.h>
#include <dbupdate.h>

struct Static_Widgets {
    Evas_Object *background;
    struct Buttom_Bottons *bottom_buttons;
};

Ecore_Evas  *   ee = NULL;
static struct Static_Widgets _static_widgets;

Evas_Object * set_background(Evas *evas) {
    int w, h;
    Evas_Object *background;
    background = assassin_edje_object_get("assassin/background");
    evas_output_viewport_get(evas, NULL, NULL, &w, &h);
    evas_output_size_set(evas, w, h);
    evas_object_resize( background,  w, h);
    evas_object_move(background, 0, 0);
    evas_object_show(background);
    return background;
}

static void on_resize(Ecore_Evas *ee) {
    Evas *evas;
    int w, h;
    struct Scroll_Page * SP;
    evas = ecore_evas_get(ee);
    evas_output_size_get(evas, &w, &h);
    evas_object_resize( _static_widgets.background,  w, h);
    evas_object_move(_static_widgets.background, 0, 0);
    auto_adjust_button_bar(_static_widgets.bottom_buttons);
    SP = current_scroll_page_get();
    if (!SP) {
        error("current scroll page is NULL!!\n");
        return;
    }
    evas_object_resize(SP->frame, w, h);
}

struct Target * target_new() {
    struct Target * T =  (struct Target*) malloc(sizeof(struct Target));
    memset(T, 0, sizeof(*T));
    return T;
}

void target_free(struct Target * target) {
    if (!target) return;
    if (target->message)
        free(target->message);
    if (target->alias)
        free(target->alias);
    free(target);
}

int gui_initialize(void *data) {
    if (!db_update_check()) {
        get_categories_page();
    }
    return 0;
}

int main (int args, char ** argv) {
    Evas        *   evas;
    system("killall packagekitd");
    if (!ecore_init()) {
        error("failed to init ecore\n");
        return -1;
    }
    if (!ecore_evas_init()) {
        error("failed to init ecore_evas\n");
        return -1;
    }
    if (!edje_init()) {
        error("failed to init edje\n");
        return -1;
    }
    if (!pk_e_dbus_init()) {
        error("failed to init pk_e_dbus\n");
        return -1;
    }
    if (!etk_init(args, argv)) {
        error("failed to init etk\n");
        return -1;
    }

    ecore_app_args_set(args, (const char **) argv);
    ee = ecore_evas_software_x11_16_new(NULL, 0,  0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    if (!ee) {
        ee = ecore_evas_software_x11_new(NULL, 0,  0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    if (!ee) {
        error("Cannot get Ecore_Evas. Please check DISPLAY\n");
        return 1;
    }
    ecore_evas_borderless_set(ee, 0);
    ecore_evas_title_set(ee, "Installer");
    ecore_evas_show(ee);
    evas = ecore_evas_get(ee);
    evas_font_path_append(evas, "fonts/");
    _static_widgets.background = set_background(evas);

    _static_widgets.bottom_buttons = new_button_bar(evas);
    ecore_evas_callback_resize_set(ee, on_resize);

    ecore_timer_add(0.01, gui_initialize, NULL);

    etk_main();
    return 0;
}
